/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edi.proxy.component
{
	/**
	 * 构造FixTable协议串 
	 * 
	 */	
	public class FixTable
	{
		/**
		 * 应用名 
		 */		
		public var appName:String="";
		/**
		 * 源字段名；主要用于与目标字段名进行对比比较 
		 */		
		public var associateFieldSource:String="";
		/**
		 *目标字段名 
		 */		
		public var associateFieldTarget:String="";

		private var mapArr:Array;
		/**
		 * eql语句 
		 */		
		public var eql:String=""
		/**
		 * 服务名 
		 */		
		public var serviceName:String="";
		private var serviceArr:Array;
		/**
		 * 根据用户ID直接进行
		 */		
		public var fixUser:String="";
		/**
		 * 
		 */		
		public var fixUnit:String="";
		
		/**
		 * 构造fixTable协议串 
		 * 
		 */		
		public function FixTable()
		{
			mapArr=new Array();
			serviceArr=new Array();
		}
		
		/**
		 * 添加映射关系 
		 * @param mapField 映射字段名
		 * @param targetField 目标字段名
		 * 
		 */		
		public function addMap(mapField:String,targetField:String):void
		{
			var obj:Object=new Object();
			obj["target"]=targetField;
			obj["map"]=mapField;
			mapArr.push(obj);
		}
		
		/**
		 * 添加服务的参数 
		 * @param name 服务参数
		 * @param value 服务参数实参
		 * 
		 */		
		public function addServiceParam(name:String,value:String):void
		{
			var obj:Object=new Object();
			obj["name"]=name;
			obj["value"]=value;
			serviceArr.push(obj);
		}
		
		/**
		 * 生成 fixTable协议串
		 * @return  
		 *  <ds>
		 *	  <caller/>
		 *	  <appName>test2</appName>
		 *	  <EPOParam>
		 *	    <dsName>a1</dsName>
		 *	    <methods>null</methods>
		 *	    <eql>select typeid,k.id id,publishpersonid,knowledgename from knowledge k</eql>
		 *	    <parameters/>
		 *	    <fixTables>
		 *	      <fixTable appName="serverapp"  fixUser="userID" fixUnit="unitID">
		 *	        <associateField source="publishpersonid" target="id"/>
		 *	        <eql>select * from edi_user</eql>
		 *	        <map mapField="pp" targetField="nickName"/>
		 *	        <service>
		 *	          <serviceName/>
		 *	          <parameters/>
		 *	        < rvice>
		 *	      </fixTable>
		 *	    </fixTables>
		 *	  </EPOParam>
		 *	</ds> 
		 */		
		public function toXml():XML
		{
			var xml:XML=XML(<fixTable appName={appName} fixUser={fixUser} fixUnit={fixUnit}/>);
			xml.appendChild(<associateField source={associateFieldSource} target={associateFieldTarget} />);
			xml.appendChild(<eql>{eql}</eql>);
			var i:int;
			for(i=0;i<mapArr.length;i++)
			{
				xml.appendChild(<map mapField={mapArr[i]["map"]} targetField={mapArr[i]["target"]} />);
			}
			var service:XML=XML(<service><serviceName>{serviceName}</serviceName></service>);
			var parameters:XML=XML(<parameters />);
			for(i=0;i<serviceArr.length;i++)
			{
				parameters.appendChild(<param name={serviceArr[i]["name"]}>{serviceArr[i]["value"]}</param>);
			}
			service.appendChild(parameters);
			xml.appendChild(service);
			return xml;
		}
		
	}
}